package org.jash.demo2205

import android.content.Intent
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.lifecycleScope
import com.hyphenate.chat.ChatClient
import com.hyphenate.helpdesk.callback.Callback
import kotlinx.coroutines.launch
import org.jash.demo2205.databinding.ActivityRegisterBinding
import org.jash.demo2205.utils.retrofit

class RegisterActivity : AppCompatActivity() {
    val binding:ActivityRegisterBinding by lazy { DataBindingUtil.setContentView(this, R.layout.activity_register) }
    val user:MutableMap<String, String> by lazy { mutableMapOf() }
    val service by lazy { retrofit.create(APIService::class.java) }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding.user = user
        binding.register.setOnClickListener {
            lifecycleScope.launch {
                val res = service.register(user)
                ChatClient.getInstance().register(user["username"]?:"", user["password"]?:"", object :
                    Callback {
                    override fun onSuccess() {
                    }

                    override fun onError(code: Int, error: String?) {
                    }

                    override fun onProgress(progress: Int, status: String?) {
                    }

                })
                if (res.code == 200) {
                    setResult(RESULT_OK, Intent().putExtra("username", user["username"]).putExtra("password", user["password"]))
                    finish()
                } else {
                    Toast.makeText(this@RegisterActivity, res.message, Toast.LENGTH_SHORT).show()
                }
            }
        }
    }
}